package routes

import (
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"os"
	"wxuser-boot/config"
	"wxuser-boot/controller/verificationcode"
	"wxuser-boot/controller/wxuser"
	swaggerDocs "wxuser-boot/docs"
)

// SetupRoutes 统一设置路由
func SetupRoutes(r *gin.Engine) {
	serverIp := getEnvOrDefault("SERVER_IP", "127.0.0.1")
	swaggerDocs.SwaggerInfo.Host = serverIp + ":" + config.GetPort()
	// 使用局部中间件
	// authMiddleware := shiro.AuthMiddleware()
	// 分组路由走中间件验证token
	// wxUserGroup := r.Group("/admin/wxUser", authMiddleware)
	// 不走中间件验证token
	// 分组路由：用户管理
	wxUserGroup := r.Group("/admin/wxUser")
	{
		wxUserGroup.POST("/add", wxuser.CreateUser)
		wxUserGroup.POST("/checkCaptcha", verificationcode.CheckCaptcha)
		wxUserGroup.GET("/queryByName", wxuser.QueryByName)
		wxUserGroup.DELETE("/deleteUserByUserNameAndAppId", wxuser.DeleteUserByUserNameAndAppId)
		wxUserGroup.GET("/getUser/:id", wxuser.GetUser)
		wxUserGroup.PUT("/update", wxuser.UpdateUser)
		wxUserGroup.DELETE("/delete/:id", wxuser.DeleteUser)
	}
	// 验证码相关路由
	r.GET("/sys/randomImage/:key", verificationcode.RandomImage)
	// Swagger 文档路由
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}

// getEnvOrDefault 获取环境变量值，如果不存在则返回默认值
func getEnvOrDefault(key, defaultValue string) string {
	value := os.Getenv(key)
	if value == "" {
		return defaultValue
	}
	return value
}
